package interactions
{
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	
	import enemies.*;
	
	/**
	 * ...
	 * @author Chris Gamble
	 */
	public class Teleporter extends Interaction
	{
		public static const USABLE:uint = 0;
		public static const COOLDOWN:uint = 1;
		
		private var _other:Entity;
		
		public function Teleporter(i:uint = 0, j:uint = 0)
		{
			super(i, j);
			
			_other = null;
			
			_timer.duration = 3000;
			
			_cooldownBar.setRange(0, _timer.duration / 1000);
			
			loadGraphic(Assets.ImgGraveUnlocked, true);
			addAnimation("default", [0], 0, false);
			addAnimation("glow", [0, 1, 2, 1], 3);
			play("glow");
			
			width = Maze.tileWidth;
			height = Maze.tileHeight;
			centerOffsets();
		}
		
		override public function update():void
		{
			super.update();
			
			if (state == COOLDOWN)
			{
				_cooldownBar.visible = true;
				if (_timer.hasExpired)
				{
					play("glow");
					state = USABLE;
					alpha = 1.0;
					_showSpacebar = true;
				}
			}
		}
		
		override public function overlapWithEnemy(e:Enemy):void
		{
			return;
		}
		
		override protected function interactWithPlayer(p:Player):void
		{
			if (state == USABLE)
			{
				activate();
				
				if (_other != null)
				{
					FlxG.play(Assets.SndTeleport, Registry.soundVolume);
					Controller.init();
					p.x = _other.x;
					p.y = _other.y;
					
					Animation.greenBoxTile(new FlxPoint(p.x, p.y));
					
					if (_other is Teleporter)
					{
						//Teleporter(_other).activate();
					}
				}
			}
		}
		
		public function set other(otherThing:Entity):void
		{
			_other = otherThing;
		}
		
		protected function activate():void
		{
			play("default");
			state = COOLDOWN;
			_showSpacebar = false;
			_timer.start();
			alpha = 0.5;
		}
	}
}